package com.gxa.weixing.pc.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.gxa.weixing.core.common.ExceptionCode;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.core.pojo.Shop;
import com.gxa.weixing.core.service.impl.BaseServiceImpl;
import com.gxa.weixing.core.util.VerifyParamUtil;
import com.gxa.weixing.pc.mapper.ShopMapper;
import com.gxa.weixing.pc.service.ShopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lijianmin
 * @date 2022/01/06  16:55:22
 */
@Service("shopService")
@Transactional(rollbackFor = Throwable.class)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    /**
     * 根据门店名称(可以为空,空则全查询)获取门店列表的方法
     *
     * @param name 门店名称
     * @param page 页码  如果不传递则为null
     * @param size 每页数据量 如果不传递则为null
     * @return 返回查询的数据
     */
    @Override
    public PageInfo<Shop> getList(String name, Integer page, Integer size) throws BusinessException {
        // 分页条件进行校验
        Integer pages = VerifyParamUtil.pageDefaultOneVerify(page);
        Integer sizes = VerifyParamUtil.sizeInfiniteVerify(size);
        // 开启分页
        PageMethod.startPage(pages, sizes);

        // 获取根据条件查询到的门店列表
        List<Shop> list = shopMapper.getList(name);
        // 返回带有分页的列表
        return PageInfo.of(list);
    }

    @Override
    public void add(Shop shop) throws BusinessException {
        // 进行是否为空校验
        VerifyParamUtil.checkParams(shop);
        // 进行添加操作
        Shop shopAdd = insert(shop);
        //如果返回结果不正确 则抛出异常
        if (shopAdd == null || shopAdd.getId() == null) {
            throw new BusinessException(ExceptionCode.ADD_ERROR);
        }
    }

    @Override
    public void delete(Long id) throws  BusinessException {
        // 进行是否为空校验
        VerifyParamUtil.checkParams(id);
        // 进行删除操作
        Integer shopDelete = deleteById(id);
        //如果返回结果不正确 则抛出异常
        if (shopDelete == null || shopDelete <= 0) {
            throw new BusinessException(ExceptionCode.DELETE_ERROR);
        }
    }

    @Override
    public void update(Shop shop) throws  BusinessException {
        // 进行是否为空校验
        VerifyParamUtil.checkParams(shop);
        // 进行修改操作
        Integer shopUpdate = updateById(shop);
        //如果返回结果不正确 则抛出异常
        if (shopUpdate == null || shopUpdate <= 0) {
            throw new BusinessException(ExceptionCode.UPDATE_ERROR);
        }
    }
}
